package com.ce.cetag.tags.validpage;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
/**
*
* <p>版权所有：中企动力
* <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
* <p>功能描述：权限面板标签
*
* @author kuanglingxuan
* Created on 2007-9-31
*/
public class ValidPageTag extends TagSupport {

	public static final String POPUP = "popup";
	public static final String IFRAME = "iframe";

	public int doStartTag() throws JspTagException{

		String pageName = ((HttpServletRequest)pageContext.getRequest()).getServletPath();
		pageName = pageName.substring(pageName.lastIndexOf("/")+1);
		System.out.println("pageName------>"+pageName);
		String valiCode = pageName + "_" + getFunction();
		System.out.println("validCodes---->"+validCodes);
		StringBuffer out = new StringBuffer();
		out.append("<script>");
		if(IFRAME.equals(this.jsValid))
			out.append("if(top == self){window.location.href = \""+this.redirectUrl+"\";}");
		else if(POPUP.equals(this.jsValid))
			out.append("if(window.dialogArguments == undefined){window.location.href = \""+this.redirectUrl+"\";}");

		out.append("</script>");
		try {

			pageContext.getOut().write(out.toString());
			if(this.validCodes != null && !this.validCodes.contains(valiCode)){
				HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();

//				out.append("alert(\"不能直接跳转，请登陆系统!\");");


				System.out.println("redirectUrl---->"+redirectUrl);
				response.sendRedirect(this.redirectUrl);

			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SKIP_BODY;
	}


	public int doEndTag() throws JspTagException{

		return EVAL_PAGE;

	}


	private List<String> validCodes;

	public List<String> getValidCodes() {
		return validCodes;
	}


	public void setValidCodes(List<String> validCodes) {
		this.validCodes = validCodes;
	}

	private String function;

	public String getFunction() {
		return function;
	}


	public void setFunction(String function) {
		this.function = function;
	}

	private String redirectUrl;

	public String getRedirectUrl() {
		return redirectUrl;
	}


	public void setRedirectUrl(String redirectUrl) {
		this.redirectUrl = redirectUrl;
	}

	private String jsValid;

	public String getJsValid() {
		return jsValid;
	}


	public void setJsValid(String jsValid) {
		this.jsValid = jsValid;
	}

}
